package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.ClassPojo;

public class ClassDao {

	public void insertClassPojo(ClassPojo classpojo) {
		DBCon db = new DBCon();
		Connection con;
		try {
			con = db.getCon();

			// First check that an identical record doesn't already exist

			if (!doesClassExist(classpojo)) {
				Statement stmt;
				stmt = con.createStatement();
				stmt
						.executeUpdate("INSERT INTO classes(class_name, class_uri, class_ns) values('"
								+ classpojo.getClassName()
								+ "','"
								+ classpojo.getClassUri()
								+ "','"
								+ classpojo.getNamespace() + "')");
			} else {
				Statement stmt;
				stmt = con.createStatement();
				stmt.executeUpdate("UPDATE classes SET class_name='"
						+ classpojo.getClassName() + "', class_uri='"
						+ classpojo.getClassUri() + "', class_ns='"
						+ classpojo.getNamespace() + "' WHERE class_uri='"
						+ classpojo.getClassUri() + "'");
			}

		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public boolean doesClassExist(ClassPojo classpojo) {
		boolean doesItExist = true;

		DBCon db = new DBCon();
		Connection con;
		try {
			con = db.getCon();

			String className = classpojo.getClassName();
			String classUri = classpojo.getClassUri();
			String classNs = classpojo.getNamespace();

			System.out.println("doesClassExist: " + classUri);

			Statement stmt;
			stmt = con.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT class_id FROM classes WHERE class_uri='"
							+ classUri + "'");
			if (rs.next()) {
				doesItExist = true;
			} else {
				doesItExist = false;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doesItExist;
	}

	public int getClassId(String uri) {

		int id = -1;
		try {
			DBCon db = new DBCon();
			Connection con;

			con = db.getCon();

			Statement stmt;
			stmt = con.createStatement();
			ResultSet rs;
			rs = stmt
					.executeQuery("SELECT class_id FROM classes WHERE class_uri='"
							+ uri + "'");
			while (rs.next()) {
				id = rs.getInt("class_id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}

	public void insertSubClass(ClassPojo cla, ClassPojo subCla) {

		// check if classes exist, if not add the classes

		String claUri = cla.getClassUri();
		System.out.println(claUri);
		String subClaUri = subCla.getClassUri();
		System.out.println(subClaUri);

		boolean classExists = true;
		boolean subClassExists = true;

		DBCon db = new DBCon();
		Connection con;

		try {
			con = db.getCon();

			Statement stmt;
			stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT class_id FROM classes WHERE class_uri='"
							+ claUri + "'");
			if (rs.next()) {
			} else {
				classExists = false;
				insertClassPojo(cla);
				System.out.println("Class inserted - hopefully");
			}

			ResultSet rs2 = stmt
					.executeQuery("SELECT class_id FROM classes WHERE class_uri='"
							+ subClaUri + "'");
			if (rs2.next()) {
			} else {
				subClassExists = false;
				insertClassPojo(subCla);
				System.out.println("Class inserted - hopefully");
			}

			int claId = getClassId(cla.getClassUri());
			System.out.println(claId);
			int subClaId = getClassId(subCla.getClassUri());
			System.out.println(subClaId);

			ResultSet rs3 = stmt
					.executeQuery("SELECT onto_id FROM ontology where superClass_id="
							+ claId + " AND subClass_id=" + subClaId + "");
			if (!rs3.next()) {
				stmt
						.executeUpdate("INSERT INTO ontology(superClass_id, subClass_id) VALUES("
								+ claId + "," + subClaId + ")");
			}

		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
